(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.popup.partial.InvList', createController);
	createController.$inject = [ '$scope', '$filter', 'Constants', '$uibModalInstance', 'dialogsModule', 'arithService', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService'];
	function createController($scope, $filter, Constants, $uibModalInstance, dialogsModule, arithService, modalService, gridService, layoutConfig, validatorConfig, authService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.piDiscountRequest = {};
		ctrl.popAmtChangedInv = {};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.popAmtChangedInv) !== false) {
				$uibModalInstance.close();
			}
			//change finance Amt in parent page
			var row = dialogsModule.params.row;
			row.financeAmt = 0;
			angular.forEach(ctrl.invoiceCriteria.list, function(data) {
				row.financeAmt = arithService.add(row.financeAmt, data.financeAmt);
			});
			//for due date discount page
			if(row.invoiceMaturitydate){
				dialogsModule.params.financeAmtByDate[row.invoiceMaturitydate] = row.financeAmt;
			}
			//for community discount page
			if(row.communityId){
				dialogsModule.params.financeAmtByCommunityId[row.communityId] = row.financeAmt;
			}
		};

		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.invoiceCriteria.list';
			ctrl.gridOptions.enableRowHeaderSelection = false;
			ctrl.invoiceCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.invoiceCriteria.storageSearchCriteria = false;
			if(dialogsModule.params.row.invoiceMaturitydate){
				ctrl.invoiceCriteria.invoiceMaturitydate = dialogsModule.params.row.invoiceMaturitydate;
			};
			if(dialogsModule.params.row.communityId){
				ctrl.invoiceCriteria.communityId = dialogsModule.params.row.communityId;
			};
			if(dialogsModule.params.amtChangedInv){
				ctrl.popAmtChangedInv = angular.copy(dialogsModule.params.amtChangedInv);
			};
			if(dialogsModule.params.effectiveDate){
				ctrl.invoiceCriteria.effectiveDate = dialogsModule.params.effectiveDate;
			};
			if(dialogsModule.params.invoiceCcy){
				ctrl.invoiceCriteria.invoiceCcy = dialogsModule.params.invoiceCcy;
			};
			if(dialogsModule.params.isFinanceAmtReadOnly){
				ctrl.invoiceCriteria.isFinanceAmtReadOnly = dialogsModule.params.isFinanceAmtReadOnly;
			};
			if(dialogsModule.params.partialFinanceOption){
				ctrl.invoiceCriteria.partialFinanceOption = dialogsModule.params.partialFinanceOption;
			};
			ctrl.search();
		};
		
		ctrl.changeFinanceAmt = function(value, formCtrl, isInitialized){
			if(!isInitialized){
				var invoiceId = $filter('encode')(value.invoiceId);
				var financeAmt = $filter('encode')(value.financeAmt);
				ctrl.popAmtChangedInv[invoiceId] = financeAmt;
			};
		};
		
		ctrl.search = function() {
			ctrl.invoiceCriteria.list = [];
			authService.search(ctrl.invoiceCriteria).then(
			function(response) {
				angular.merge(ctrl.invoiceCriteria, response.data);
				angular.forEach(ctrl.invoiceCriteria.list, function(data) {
					if(data.communityCodeQ != null && data.communityCode != null){
						data.community = data.communityCodeQ + ":" + data.communityCode;
					}
					//change finance Amt
					if(ctrl.popAmtChangedInv.hasOwnProperty($filter('encode')(data.invoiceId))){
						data.financeAmt = ctrl.popAmtChangedInv[$filter('encode')(data.invoiceId)];
					}else{
						data.financeAmt = data.financeAvlAmt;
					}
					data.isFinanceAmtReadOnly = ctrl.invoiceCriteria.isFinanceAmtReadOnly;
				});
				gridService.updateGridOptions(ctrl.invoiceCriteria, ctrl.gridOptions);
			},angular.noop);
		
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.invoiceCriteria.pageNum = pageNum;
			ctrl.invoiceCriteria.pageSize = pageSize;
			ctrl.invoiceCriteria.list = [];
			ctrl.search();
		}

		ctrl.createContinue = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('apf.finance.preview', ctrl.selectedRows);
		};

		
		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.invoiceCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.invoiceCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}
	}
})(angular);